#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：python常用模块库 
@File ：monitor.py
@IDE  ：PyCharm 
@Author ：李涵彬
@Date ：2025/2/21 上午9:03 
"""

from pathlib import Path
from typing import Dict

from .scanner import FileScanner


class FileMonitor:
	def __init__(self, config: Dict):
		self.config = config
		self.scanner = FileScanner(config.get("max_workers", 4))

	def check_thresholds(self) -> Dict:
		"""检查所有监控目录"""
		results = {}
		for dir_config in self.config["directories"]:
			path = Path(dir_config["path"])
			files = list(self.scanner.scan(path, dir_config["exclude"]))
			total_size = sum(f.result().get("size", 0) for f in files if not f.exception())

			results[str(path)] = {
				"file_count": len(files),
				"total_size": total_size,
				"status": self._evaluate_status(dir_config, len(files), total_size)
			}
		return results

	def _evaluate_status(self, config: Dict, count: int, size: int) -> str:
		max_files = config["max_files"]
		max_size = config["max_size_gb"] * 1024 ** 3
		if count > max_files or size > max_size:
			return "CRITICAL"
		elif count > 0.8 * max_files or size > 0.8 * max_size:
			return "WARNING"
		return "OK"
